﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Core
{
    public partial class Language : CMSEntity<Language, string>
    {
        public static Language Default
        {
            get
            {
                var language = Table.FirstOrDefault(e => e.IsDefault);
                if (language == null)
                {
                    language = new Language()
                    {
                        IsDefault = true,
                        Title = "English",
                        LanguageId = "en"
                    }.Save();
                }
                return language;
            }
        }

        public static Language Current
        {
            get
            {
                var l = ContextItem.GetItem<Language>("Language.Current", () => null);
                if (l == null)
                {
                    l = Load(SessionItem.GetItem("Language.CurrentId", () => Language.Default.LanguageId));
                    ContextItem.SetItem("Language.Current", l);
                }
                return l;
            }
            set
            {
                SessionItem.SetItem("Language.CurrentId", value.LanguageId);
                ContextItem.SetItem("Language.Current", value);
            }
        }

        public override System.Linq.Expressions.Expression<Func<Language, bool>> GetIDSelector(string ID)
        {
            return e => e.LanguageId == ID;
        }

        public override string GetKey(Language Entity)
        {
            return Entity.LanguageId;
        }

        public override bool IsNew(Language Entity)
        {
            return Table.FirstOrDefault(e => e.LanguageId == Entity.LanguageId) == null;
        }

        public override string ToString()
        {
            return this.LanguageId + " - " + this.Title + (this.IsDefault ? " (default)" : "");
        }
    }
}
